package com.mew.chess.core.game.shbz.game.common;

import cn.hutool.core.util.RandomUtil;
import com.mew.common.util.NumberUtil;
import com.zebra.common.ByteBufUtils;
import io.netty.buffer.ByteBuf;
import lombok.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class FishBO {

    private int fishId;
    private int fishType;  // 鱼类型ID
    private int ruleStep;     // 规定步长
    private int remainStep;   // 行走步长；
    private long startTime;
    private int routeId;         // 路线
    //private byte rollback;  //0 正序  1 反序
    private float[] pointsSpeed;
    private int fishIdx;  // 鱼类型ID

    public int getRemainStep(){
        //单位帧
        if (0 == this.startTime){
            this.startTime = System.currentTimeMillis();
            return 0;
        }

        return FishTableUtil.computeRemainStep(this.startTime);
    }


    public static FishBO creat(int fishId, Route route){
        FishBO fishBO = new FishBO();
        fishBO.fishId = fishId;
        fishBO.routeId = route.getLineID();
        fishBO.ruleStep = route.getRootLength();
        fishBO.startTime = System.currentTimeMillis();
        fishBO.fishType = route.getFishType()[RandomUtil.randomInt(route.getFishType().length)];
        List<Float> list = Arrays.stream(route.getPointsSpeed()).map(i -> (float) NumberUtil.randomBigDecimalByStr(i, "-").floatValue()).collect(Collectors.toList());
        float[] floats = new float[list.size()];
        for (int i = 0; i < floats.length; i++) {
            floats[i] = list.get(i);
        }
        fishBO.pointsSpeed = floats;

        return fishBO;
    }

    public static FishBO creat(int fishId,int fishType,int routeId,int fishIdx){
        FishBO fishBO = new FishBO();
        fishBO.fishId = fishId;
        fishBO.routeId = routeId;
        fishBO.ruleStep = 60000;
        fishBO.startTime = System.currentTimeMillis();
        fishBO.fishType = fishType;
        fishBO.pointsSpeed = new float[0];
        fishBO.fishIdx = fishIdx;
        return fishBO;
    }

    public void write(ByteBuf b){
        ByteBufUtils.writeInt(b,fishId);
        ByteBufUtils.writeInt(b,fishType);
        ByteBufUtils.writeInt(b,getRemainStep());
        ByteBufUtils.writeInt(b,routeId);
        ByteBufUtils.writeFloatArray(b,pointsSpeed);
    }

    public void writeTide(ByteBuf b){
        ByteBufUtils.writeInt(b,fishId);
        ByteBufUtils.writeInt(b,routeId);
        ByteBufUtils.writeInt(b,fishIdx);

    }

}
